/***************************************************************************
 创建者：华磊
 开始时间：         : 2020.12.17
 copyright            : (C) 深圳市华友高科有限公司
 修改说明：（每次有修改就添加一条，带有 修改人，修改时间，修改描述）

 ***************************************************************************
 *   选择指令属性编辑视窗
*                                                                         *
 ***************************************************************************/
#include "selectinstructiondelegate.h"
#include <QDebug>

SelectInstructionDelegate::SelectInstructionDelegate(QWidget *parent):
    QObject(parent)
{
}

void SelectInstructionDelegate::initialView()
{
    selectInstructionView->selectInstruction = "SELECT";

    selectInstructionView->rIndex = "1";
    selectInstructionView->rValue = "1 ";
    selectInstructionView->dealWith = "JMP LBL";
    selectInstructionView->dealWithType = 1;
    selectInstructionView->lblIndex = "1";
    selectInstructionView->callProgram = "file";
    selectInstructionView->callLocation = "./";
    selectInstructionView->note = "";

    //selectInstructionView->updateView();
}

void SelectInstructionDelegate::selectJmpDealSlot()
{
    selectInstructionView->dealWithType = 0;

    qDebug("select JMP deal");
}

void SelectInstructionDelegate::selectCallDealSlot()
{
    selectInstructionView->dealWithType = 1;

    qDebug("select CALL deal");
}

void SelectInstructionDelegate::browseCallProgramSlot(QString callLocation)
{
//    QString filePath;
//    QFileInfo  fileInfo;
//    filePath = QFileDialog::getOpenFileName(selectInstructionView, tr(""), callLocation, tr(""));
//    fileInfo = QFileInfo(filePath);

//    selectInstructionView->callProgram = fileInfo.fileName();
//    selectInstructionView->updateView();



    QFileDialog dialog(selectInstructionView,"");
    dialog.resize(580,400);
    dialog.setAcceptMode(QFileDialog::AcceptOpen);
    dialog.setNameFilter("TXT(*.txt)");
    dialog.setDirectory(callLocation);

    QStringList files = dialog.selectedFiles();
    if(!files.isEmpty())
    {
        if(dialog.exec() == QFileDialog::Accepted)
        {
            qDebug()<<"ok";
            QStringList name = dialog.selectedFiles();

            if(1 == name.size()){
                QStringList nameList = name[0].split("/");

                selectInstructionView->callProgram = nameList[nameList.size()-1];
                selectInstructionView->updateView();
            }
        }
        else
        {
            qDebug()<<"close";
        }
    }

    qDebug("browse call program");
}

void SelectInstructionDelegate::clearNoteSlot()
{
    selectInstructionView->note.clear();

    qDebug("clear note");
}

void SelectInstructionDelegate::finishEditInstructionSlot()
{
    QString str = "SELECT ";
    selectInstructionView->selectInstruction = str + "R" + "[" + selectInstructionView->rIndex + "]"
            + "=" + selectInstructionView->rValue +  ", " ;

    if(0 == selectInstructionView->dealWithType)
    {
        QString str = "JMP LBL";
        selectInstructionView->dealWith = str + "[" + selectInstructionView->lblIndex + "]";
    }
    else
    {
        selectInstructionView->dealWith = "CALL " + selectInstructionView->callProgram;
    }

    selectInstructionView->selectInstruction += selectInstructionView->dealWith + "; " + "//" + selectInstructionView->note;
}
